<?php
$path = null;
$req = array();

if(isset($argv)){
	$path = $argv[0];
	$key = $argv[1];
	$secret = $argv[2];
	
}else{
	$path = $_GET['method'];
	$key = $_GET['public'];
	$secret = $_GET['private'];
	
}

switch($path){
case "getinfo":
case "getmarkets":
case "getcoindata":
case "mytransactions":
case "getwalletstatus":
case "allmyorders":
case "cancelallorders":
case "mytransfers":
case "getmydepositaddresses":
	echo api_query($key, $secret, $path);
	
	break;

case "markettrades":
case "marketorders":
case "myorders":
case "depth":
case "cancelmarketorders":
	$req["marketid"] = get_value_check_null("marketid");
	echo api_query($key, $secret, $path, $req);

	break;

case "mytrades":
	$req["marketid"] = get_value_check_null("marketid");
	$req["limit"] = get_value_check_null("limit");
	echo api_query($key, $secret, $path, $req);

	break;
	
case "allmytrades":
	$req["startdate"] = get_value_check_null("startdate");
	$req["enddate"] = get_value_check_null("enddate");
	echo api_query($key, $secret, $path, $req);

	break;
	
case "createorder":
	$req["marketid"] = get_value_check_null("marketid");
	$req["ordertype"] = get_value_check_null("ordertype");
	$req["quantity"] = get_value_check_null("quantity");
	$req["price"] = get_value_check_null("price");
	echo api_query($key, $secret, $path, $req);
	
	break;
	
case "cancelorder":
case "getorderstatus":
	$req["orderid"] = get_value_check_null("orderid");
	echo api_query($key, $secret, $path, $req);

	break;
	
case "calculatefees":
	$req["ordertype"] = get_value_check_null("ordertype");
	$req["quantity"] = get_value_check_null("quantity");
	$req["price"] = get_value_check_null("price");
	echo api_query($key, $secret, $path, $req);
	
	break;

case "generatenewaddress":
	$currencyid = get_value_check_null("currencyid");
	if(is_null($currencyid)){
		$req["currencycode"] = get_value_check_null("currencycode");
	
	}else{
		$req["currencyid"] = $currencyid;
	
	}
	
	echo api_query($key, $secret, $path, $req);
	
	break;
	
case "makewithdrawal":
	$req["address"] = get_value_check_null("address");
	$req["amount"] = get_value_check_null("amount");
	
	echo api_query($key, $secret, $path, $req);

	break;
	
}

function api_query($key, $secret, $method, array $req = array()) {
    
    $req['method'] = $method;
    $mt = explode(' ', microtime());
    $req['nonce'] = $mt[1];
    
    // generate the POST data string
    $post_data = http_build_query($req, '', '&');

    $sign = hash_hmac("sha512", $post_data, $secret);
 
    // generate the extra headers
    $headers = array(
            'Sign: '.$sign,
            'Key: '.$key,
    );
 
    // our curl handle (initialize if required)
    static $ch = null;
    if (is_null($ch)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Cryptsy API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
    }
    curl_setopt($ch, CURLOPT_URL, 'https://api.cryptsy.com/api');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 
    // run the query
    $res = curl_exec($ch);

    //if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
    //$dec = json_decode($res, true);
    //if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
    return $res;
}

function get_value_check_null($name){
	$val = NULL;
	if(isset($_GET[$name])){
		$val = $_GET[$name];
		if($val == "null")
			$val = NULL;
	}
	return $val;
}

?>